home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Rewrite 0.2.6 / ReWrite 0.2.6 Docs / ReWrite 0.2.6 Docs.rsrc / TEXT_141.txt < prev    next >
Encoding:
Text File  |  1995-08-23  |  6.7 KB  |  141 lines

  1.  
  2. Appendix 5 - Efficient Coding
  3.  
  4. This section gives some ideas about how to get the most speed and memory efficiency out of ReWrite code.
  5.  
  6. ‚Ä¢ Be careful about extra copies of structures
  7.  
  8.    Because of the feature of unique references (a copy of a structure for each reference to it), it is easy to get ReWrite spending most of its time needlessly copying and destroying lists. It is possible to avoid this, possibly at the expense of somewhat more convoluted code (see the nprime example).
  9.    Another example is this (poorly written) function to insert an element into the nth position of a list:
  10.  
  11. insertnth[x,0,l] -> {x,.l};
  12. insertnth[x,n,l] -> {car[l],.insert[x,n-1,cdr[l]};
  13.  
  14. This will make a copy of the list l and destroy it n times - most inefficient.
  15.  
  16. A better version would be:
  17.  
  18. insertnth[x,0,l] -> {x,.l};
  19. insertnth[x,n,{a,.rest}] -> {a,.insert[x,n-1,rest]};
  20.  
  21.    This is not much of a problem with atoms - they are still copied and destroyed, but this is very quick (see declaring types below).
  22.    A later version of ReWrite may spot some of the obvious places where this is unnecessary, and improve the code internally.
  23.  
  24. ‚Ä¢ How to avoid a 'stack full' error
  25.  
  26. In the example above,
  27.  
  28. insertnth[x,0,l] -> {x,.l};
  29. insertnth[x,n,{a,.rest}] -> {a,.insert[x,n-1,rest]};
  30.  
  31. each function call goes deeper onto the stack, since the outside function on the right hand side is list not insertnth, so tail recursion cannot be used. This means that insertnth would fill up the stack if it was used with a long list and large n. It is possible to rearrange this to make tail recursion possible.
  32.  
  33. insertnth[x,n,l] -> insertnthx[x,n,{},l];
  34.  
  35. (* insertnthx has an extra parameter to store the list elements that will appear to the left of x *)
  36.  
  37. insertnthx[x,0,l,rest] -> {.l,x,.rest};
  38. insertnthx[x,n,l,{a,.rest}] ->
  39.           insertnthx[x,n-1,{.l,a},rest];
  40.  
  41.    This allows tail recursion to work, and very little stack space will be used. The basic technique here is to have an extra parameter to collect the 'bits done so far' then apply them all at once, rather than one at a time.
  42.    Unlike lisp (which is slow with the appending of lists), ReWrite appends and prepends to lists at the same speed, so both versions of insertnth are nearly equally efficient (for speed).
  43.  
  44. ‚Ä¢ Use conditions sparingly
  45.  
  46.    If something can be done with either a condition of a match, the match is much clearer and faster. A problem with conditions is that all parameters used in them need to have copies made of them, because they may not damage the argument list until a particular rule has been chosen.
  47.    In the factorial example,
  48.  
  49. factorial[0] -> 1;
  50. factorial[n] -> n * factorial[n-1];
  51.  
  52. (Time taken to find factorial[12] 10000 times on LC III: 162 ticks)
  53.  
  54. the 0 was included as part of the match. The following would be equivalent:
  55.  
  56. factorial[n]::n=0 -> 1;
  57. factorial[n] -> n * factorial[n-1];
  58.  
  59. (Time taken to find factorial[12] 10000 times on LC III: 389 ticks)
  60.  
  61. except that ReWrite handles matching more efficiently than conditions, so the second version will be much slower.
  62.  
  63. ‚Ä¢ Declare types if known
  64.  
  65.    ReWrite does not insist on types (unless they are being used as part of a match). However, type checking is very quick, and can save a lot of work as detailed below, so are worth using if a parameter is only supposed to have one particular type anyway.
  66.  
  67.    The compiler can use type information to produce more efficient code in three ways:
  68.  
  69. ‚Ä¢ Without types, any copy, compare, destroy has to call a subroutine to do so on arbitrary types. With types, there is quite often a shortcut that can be used, and a small piece of code to do just the desired operation can be put inline.
  70.  
  71. ‚Ä¢ Some functions (noted with a * to the left in the Function List Appendix) can be optimised to be inline if the type is known.
  72. For example:
  73.    splice produces inline code if it is know that it is working on a list.
  74.    mult can be greatly improved if both the arguments are known to be integers - there is a single machine code instruction to do this, and often the cell structure can be dispensed with completely, leading to very fast code.  Also in this case, the result of mult is taken to be an integer.
  75.  
  76. ‚Ä¢ If all the functions in an expression are inline, as described in the last point, a whole expression can be optimised. This can be particularly useful for conditions on rules. Normally conditions are slow (see above), but if enough is known about the functions and types in a condition, they speed up dramatically.
  77.  
  78. For example:
  79.  
  80. factorial[0] -> 1;
  81. factorial[n:int] -> n * factorial[n-1];
  82.  
  83. (Time taken to find factorial[12] 10000 times on LC III: 97 ticks)
  84.  
  85. and
  86.  
  87. factorial[n:int]::n=0 -> 1;
  88. factorial[n:int] -> n * factorial[n-1];
  89.  
  90. (Time taken to find factorial[12] 10000 times on LC III: 107 ticks)
  91.  
  92. run at about the same speed (but the first is still clearer).
  93.  
  94. To summarise:
  95.  
  96. Disadvantages of typing parameters:
  97.    - Slightly more verbose code;
  98.    - Small overhead in type checking (typically about 2 machine code instructions).
  99.  
  100. Advantages of using types:
  101.    - Code can be clearer;
  102.    - The compiler can make some safe assumptions and produce much better code.
  103.    - Errors might be spotted earlier and more easily.
  104. For example with:
  105. square[x:int] -> x*x;
  106. the type :int means that any error will be a mismatch in square rather than mult.
  107.  
  108. ‚Ä¢ Use the built in functions where possible
  109.  
  110.    Because speed has been one of the main, objectives when code has been written, the internal functions are mostly designed to run quickly. For example, if you are using sets that contain non-negative integers, use the oset*** functions.
  111.  
  112.    One thing to note when accessing lists: the treetake function is significantly faster than the nth function for anything but very small values of n, so
  113.  
  114. treetake[{n},lis] is much more efficient than nth[n,lis].
  115.  
  116. This is because treeslicesplice has been direct coded, whereas nth is written in ReWrite.
  117.  
  118. ‚Ä¢ Use splice sparingly, but whenever useful
  119.  
  120.    The splice operator is very efficient when it is used on the last pattern in a rule lhs or list, for example:
  121.  
  122.   test[{1,.r1},.r2] -> true;
  123.  
  124. will be very efficient.
  125.  
  126.    The splice operator will be somewhat less efficient when used in other situations, as a match will have to be searched for, but usually still more efficient that any alternative coding that involves searching.
  127.  
  128. For example,
  129.  
  130.   ismember[x,{._,x,._}] -> true;
  131.   ismember[x,y:lis] -> false;
  132.  
  133. while still requiring a search in the first rule, is still about 2-3 times as efficient as the following:
  134.  
  135.   ismember[x,{}] -> false;
  136.   ismember[x,{x,.rest}] -> true;
  137.   ismember[x,{y,.rest}] -> ismember[x,rest];
  138.  
  139.    So: use the splice operator whenever it is useful, but remember that it does have a time overhead. An algorithm that avoids searching altogether (if possible) will be faster (for example, pull a list to bits from the start rather than from the end).
  140.  
  141.